<?php
require_once 'CMS/Util.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/Language.php';
require_once 'CMS/User.php';
//Người tạo : Nguyễn Huỳnh Đại Thành - Team 2
//Chỉnh sửa : Bùi Thành Nguyên -Team 4

class Relative_IndexController {

	public function init(){
		parent::init();

	}

	public function indexAction()
	{
		$path = APPLICATION_PATH."/widgets/relative/";
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);//Read config.xml
		/************ Start check user login *********/
		$session1 = new Zend_Session_Namespace('login.auth');
		if(isset($session1->id_user))
		{
			$this->view->login = "1";
		
			/************ Start check permission *********/
			$user = new User();
			$user->init();
			$hasmodify = $user->modifiy_content($xmlconfig->name);
			$this->view->permission = $hasmodify;
			/************ End check user permission *********/
		}
		else 
		{
			
			$this->view->login = "0";
		}
		/************ End check user login *********/
		
		/*========== Start Load Language ==========*/
		$language = new Language(APPLICATION_PATH . '/widgets/relative');
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
			$language->load($name['name']);
		}
		else
			$language->load('Default');		
		/*========== End Load Language ==========*/
			
		/*========== Start Get Text ==============*/
		$this->view->textMore 		= $language->get('text_more');
		$this->view->textNoInfo 	= $language->get('text_noinfo');
		/*========== End Text ==============*/
		
		//Start------Khai báo các parameter
		$tbThings = new Relative_Model_Thing();
		$dateInt = new Util();
		$datathings=0;
		$type=0;
		$category=0;
		
		//End--------Khai báo các parameter
		if(count($_GET)&& isset($_GET["id"]))
		{
			$id=$_GET["id"];
			if($dateInt->is_interger($id))
			{
				$things=$tbThings->getThingById($id);
				if($things!=null)
				{
					foreach ($things as $thing)
					{
						$type=$thing["type"];
						$category=$thing["category_id"];
						$this->view->test=$type;
						$this->view->test2=$category;
						$datathings=$tbThings->getRelatedTypedThing($id, $category, $type);
						
					}
				}
			}
			
		}
		else
		{
			$datathings=$tbThings->getrandomthing();
			$type=0;
		}
		if($datathings!=null)
		{
		
			switch ($type)
			{
				case 0 :
				{
					$this->view->rtype=1;
				}
				case 1:
					{
						$this->view->products=$datathings;
						$ptitleXML = 'ptitle'.$name['name'];
						if(isset($xmlconfig->$ptitleXML))
							$this->view->title = $xmlconfig->$ptitleXML;
						else
							$this->view->title = $xmlconfig->ptitleDefault;
						//end----truyen title
						$this->view->rtype=1;
						//End--------Truyền parameter qua View
						break;
					}
				case 2:
					{
						$i = 0 ;
						foreach ($datathings as $datathing)
						{
							$title_lang=unserialize($datathing["title"]);
							$short_des_lang=unserialize($datathing["short_description"]);
							if(isset($title_lang[$name['name']]))
								$datathings[$i]["title"]=$title_lang[$name['name']];
							else
								$datathings[$i]["title"]=$title_lang["Default"];
							if(isset($short_des_lang[$name['name']]))
								$datathings[$i]["short_description"]=$short_des_lang[$name['name']];
							else
								$datathings[$i]["short_description"]=$short_des_lang["Default"];
								$i++;
						}
						$ntitleXML = 'ntitle'.$name['name'];
						if(isset($xmlconfig->$ntitleXML))
							$this->view->title = $xmlconfig->$ntitleXML;
						else
							$this->view->title = $xmlconfig->ntitleDefault;
							//end----truyen title
						$this->view->rtype=2;
						//End--------Truyền parameter qua View
						$this->view->newsR=$datathings;
						break;
					}
				case 3:
					{
						$i=0;
						foreach ($datathings as $datathing)
						{
							$title_lang=unserialize($datathing["title"]);
							if(isset($title_lang[$name['name']]))
								$datathings[$i]["title"]=$title_lang[$name['name']];
							else
								$datathings[$i]["title"]=$title_lang["Default"];
								$i++;
						}
						$qtitleXML = 'qtitle'.$name['name'];
						if(isset($xmlconfig->$qtitleXML))
							$this->view->title = $xmlconfig->$qtitleXML;
						else
							$this->view->title = $xmlconfig->qtitleDefault;
							//end----truyen title
						$this->view->rtype=3;
						//End--------Truyền parameter qua View
						$this->view->QAs=$datathings;
						break;
					}
			}
		}
		
		
	}

	
	
}